package com.xykj.enterprise.wechat.ext.api.schedule;

import com.xykj.enterprise.wechat.bean.ext.schedule.GetScheduleVo;
import com.xykj.enterprise.wechat.ext.service.schedule.ScheduleService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Function(description = "获取日历下的日程列表", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),

        @Parameter(name = "cal_id", type = Type.String, description = "日历ID", required = true),
        @Parameter(name = "offset", type = Type.String, description = "分页，偏移量, 默认为0", required = false),
        @Parameter(name = "limit", type = Type.String, description = "分页，预期请求的数据量，默认为500，取值范围 1 ~ 1000", required = false),
})
@Component
public class GetScheduleByCalendar implements Action {

    @Autowired
    private ScheduleService scheduleService;

    @Override
    public Response execute(Request request) throws Exception {
        String cal_id = request.getString("cal_id");
        Integer offset = request.getInteger("offset");
        Integer limit = request.getInteger("limit");
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cal_id", cal_id);
        if (offset != null) {
            params.put("offset", offset);
        }
        if (limit != null) {
            params.put("limit", limit);
        }
        GetScheduleVo rsp = scheduleService.getScheduleByCalendar(
                request.getString("access_token"),
                params
        );
        if (rsp.getErrcode() != 0) {
            return Response.fail(rsp.getErrmsg());
        }
        return Response.success().put("data", rsp);
    }
}
